package queue;
/*
* 简单数组队列的实现
* */
public class MyQueue {
    public static void main(String[] args) {
        MyFirstQueue queue = new MyFirstQueue(5);
        queue.push(1);
        queue.push(2);
        queue.push(3);
        queue.push(4);
        queue.push(5);
        queue.toList();
        System.out.println("======================");
        int pop = queue.pop();
        queue.pop();
        queue.pop();
        queue.pop();
        queue.pop();
        queue.pop();



        queue.toList();
    }
}

class MyFirstQueue{
    private int rear = -1;
    private int front = -1;
    private int maxSize;
    private int[] queue;
    public MyFirstQueue(int maxSize){
        this.maxSize = maxSize;
        queue = new int[maxSize];
    }

    //入队列
    public void push(int num){
        //判断是否为满
        if (isFull()){
            throw new RuntimeException("队列已经满了");
        }else{
            rear++;
            queue[rear] = num;
        }
    }

    //出队列
    public int pop(){
        //判断队列是否为空
        if (isEmp()){
            throw new RuntimeException("队列为空");
        }else{
            front++;
            return queue[front];
        }
    }

    //判断是否为满 true 为满
    public boolean isFull(){
        return (rear+1)==maxSize;
    }
    //判断队列是否为空 true 为空
    public boolean isEmp(){
        return rear==front;
    }
    //遍历队列
    public void toList(){
        if (isEmp()){
            System.out.println("队列为空");
        }else {
            for (int i = front+1;i<=rear;i++){
                System.out.println("队列的第"+i+"数为"+queue[i]);

            }
        }
    }
}
